﻿/// <reference path="../../js/viewmodels.js" />
// For an introduction to the Search Contract template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232512

// TODO: Add the following script tag to the start page's head to
// subscribe to search contract events.
//  
// <script src="/pages/searchresultbykeyword/searchresults.js"></script>

(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var appModel = Windows.ApplicationModel;
    var nav = WinJS.Navigation;
    var ui = WinJS.UI;
    var searchPageURI = "/pages/searchresultbykeyword/searchresults.html";

    var searchPane = appModel.Search.SearchPane.getForCurrentView();

    searchPane.placeholderText = "Search by keyword";
    searchPane.showOnKeyboardInput = true;

    ui.Pages.define(searchPageURI, {

        ready: function (element, options) {
            WinJS.Binding.processAll(element, ViewModels);
            ViewModels.submitSearchText(options.queryText);

            var backButton = document.getElementById("cmdBack").winControl;
            var homeButton = document.getElementById("cmdHome").winControl;
            var listView = document.getElementById("jobOffers").winControl;
            var showInfo = document.getElementById("cmdLoadInfo");

            showInfo.addEventListener("click", function () {

                var text = "";

                listView.selection.getItems().then(function (items) {
                    if (items.length != 0) {

                        for (var i = 0; i < items.length; i++) {
                            text += "<h2>\nИме: " + items[i].data.title + "\n" + "</h2>";
                            text += "<h3>\nОписание: " + items[i].data.content + "\n" + "<h3>";
                            text += "<h3>\nАдрес: <a href=" + items[i].data.link + "\n" + ">" + items[i].data.link + "</a>\n\n<h3>";
                            text += "<br/>"
                        }
                    }

                    Data.saveText(text);
                });

                nav.navigate("/pages/loaded/loadedInfo.html");
            });

            backButton.addEventListener("click", function () {
                nav.back(1);
            });

            homeButton.addEventListener("click", function () {
                nav.navigate("/pages/home/home.html");
            });
        }
    });

    WinJS.Application.addEventListener("activated", function (args) {
        if (args.detail.kind === appModel.Activation.ActivationKind.search) {
            args.setPromise(ui.processAll().then(function () {
                if (!nav.location) {
                    nav.history.current = { location: Application.navigator.home, initialState: {} };
                }

                return nav.navigate(searchPageURI, { queryText: args.detail.queryText });
            }));
        }
    });

    searchPane.onquerysubmitted = function (args) { nav.navigate(searchPageURI, args); };
})();
